package com.phs.main.web.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.phs.main.common.constant.AjaxResult;
import com.phs.main.web.po.SysUsersRoles;
import com.phs.main.web.service.ISysUsersRolesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户角色 前端控制器
 * </p>
 *
 * @author Trent
 * @since 2020-07-07
 */
@Api
@RestController
@RequestMapping("/sysUsersRoles")
public class SysUsersRolesController {
	@Lazy
	@Autowired
	ISysUsersRolesService SysUsersRolesService;


	@ApiOperation(value = "添加用户角色", response = SysUsersRoles.class)
	@PostMapping("/addSysUsersRoles")
	public AjaxResult addSysUsersRoles(@RequestBody SysUsersRoles SysUsersRoles) {
		try {
			SysUsersRolesService.save(SysUsersRoles);
			return AjaxResult.success("添加成功");
		} catch (Exception e) {
			return AjaxResult.error(e.getMessage());
		}
	}

	@ApiOperation(value = "删除", notes = "根据ID删除用户角色")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "用户角色ID", dataType = "Long", required = true),
	})
	@DeleteMapping("deleteSysUsersRolesById/{id}")
	public AjaxResult deleteSysUsersRolesById(@PathVariable long id) {
		try {
			if (SysUsersRolesService.removeById(id)) {
				return AjaxResult.success("已删除");
			} else {
				return AjaxResult.error("未删除");
			}
		} catch (Exception e) {
			return AjaxResult.error("出错", e.getMessage());
		}
	}

	@ApiOperation("更新用户角色")
	@PutMapping("/updateSysUsersRoles")
	public AjaxResult updateSysUsersRoles(@RequestBody SysUsersRoles SysUsersRoles) {
		try {
			if (SysUsersRolesService.updateById(SysUsersRoles)) {
				return AjaxResult.success("更改成功");
			} else {
				return AjaxResult.error("更改失败");
			}
		} catch (Exception e) {
			return AjaxResult.error("出错", e.getMessage());
		}
	}

	@ApiOperation("查询所有用户角色")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current", value = "页码", dataType = "Long"),
			@ApiImplicitParam(name = "size", value = "单页条数", dataType = "Long")
	})
	@GetMapping("/findAll/{current}/{size}")
	public AjaxResult findAll(@PathVariable Long current, @PathVariable(required = false) Long size) {
		if (current == null) {
			current = 1L;
		}
		if (size == null) {
			size = 10L;
		}
		Page<SysUsersRoles> page = new Page<>(current - 1, size);

		return AjaxResult.success(SysUsersRolesService.page(page));
	}

}
